//
//  VideoListItemTabCell.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/17.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit

@objc protocol DoubleLabellDelegate {
    @objc optional func doubleLabel(onDoubleCilck label:UILabel , isShow:Bool , contentHeight:CGFloat)
}

class VideoListItemTabCell: UITableViewCell , UpvoteViewDelegate{
    var headerImg:UIImageView!
    var nameLab:UILabel!
    var contentLab:UILabel!
    var collectionView:UICollectionView!
    var tagView:UIButton!
    var upvoteView:UpvoteView!
    var godCommentView:UIView!
    var godCommentLab:UILabel!
    var godTagView:UILabel!
    var commentToolbarView:CommentToolbarView!
    
    fileprivate var doubleClickBtn:UIButton!
    fileprivate var lineView:UIView!
    
    fileprivate var isMoved:Bool = false
    
    weak var delegate:DoubleLabellDelegate!
    
    fileprivate var url = ""
    fileprivate var video:Video!

    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initializeView() -> Void{
        
        self.headerImg = UIImageView()
        self.headerImg.frame = CGRect.init(x: 20, y: 20, width: 30, height: 30)
        self.contentView.addSubview(self.headerImg)

        self.headerImg.layer.masksToBounds = true
        self.headerImg.layer.cornerRadius = 15
        self.headerImg.image = UIImage.init(named: "test_header")
        
        self.nameLab = UILabel()
        self.nameLab.frame = CGRect.init(x: self.headerImg.frame.maxX + 10 , y: self.headerImg.frame.minY + 7.5, width: 100, height: 15)
        self.nameLab.font = UIFont.LightDefaultFont(12)
        self.nameLab.textColor = UIColor.m_textLightColor()
        self.nameLab.text = "虾酱大师傅"
        self.contentView.addSubview(self.nameLab)

        
        self.contentLab = UILabel()
        self.contentLab.isUserInteractionEnabled = false
        self.contentLab.frame = CGRect.init(x: self.nameLab.frame.minX, y: self.headerImg.frame.maxY + 5, width: AppWidth - 80, height: 20)
        self.contentLab.font = UIFont.LightDefaultFont(16)
        self.contentLab.textColor = UIColor.m_textDeepColor()
        self.contentLab.lineBreakMode = .byCharWrapping
        self.contentLab.text = "虾酱大师傅"
        self.contentView.addSubview(self.contentLab)
        
        self.doubleClickBtn = UIButton.init(type: .custom)
        self.doubleClickBtn.backgroundColor = UIColor.clear
        self.doubleClickBtn.setTitle("", for: UIControlState())
        self.doubleClickBtn.isUserInteractionEnabled = true
        self.doubleClickBtn.addTarget(self, action: #selector(self.doubleTapAction(sender:event:)), for: UIControlEvents.touchDownRepeat) //双击
        self.doubleClickBtn.addTarget(self, action: #selector(self.tabButtonTapped(sender:event:)), for: .touchDown)//单击
        self.contentView.addSubview(self.doubleClickBtn)
        self.doubleClickBtn.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalTo(self.contentLab)
        }
        
        let flowLayout = self.initCollectionCellStyle(self.contentLab.frame.size.width, cellHeight: 100, minimumLineSpacing: 0)
        let rect = CGRect.init(x: self.contentLab.frame.minX, y: self.contentLab.frame.maxY + 10, width: self.contentLab.frame.size.width, height: 100)
        self.collectionView = UICollectionView.init(frame: rect , collectionViewLayout: flowLayout)
        self.collectionView.backgroundColor = UIColor.green
        self.contentView.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentLab)
            make.top.equalTo(self.contentLab.snp.bottom).offset(10)
            make.width.equalTo(self.contentLab)
            make.height.equalTo(100)
        }
        
        self.tagView = UIButton.init(type: .custom)
        self.tagView.backgroundColor = UIColor.m_tagBgColor()
        self.contentView.addSubview(self.tagView)
        self.tagView.setTitleColor(UIColor.white, for: UIControlState())
        self.tagView.setTitle("看世界", for: UIControlState())
        self.tagView.titleLabel?.font = UIFont.LightDefaultFont(10)
        self.contentView.addSubview(self.tagView)
        self.tagView.snp.makeConstraints { (make) in
            make.top.equalTo(self.collectionView.snp.bottom).offset(5)
            make.left.equalTo(self.collectionView)
            make.height.equalTo(20)
            make.width.equalTo(50)
        }
        
        self.upvoteView = UpvoteView()
        self.upvoteView.backgroundColor = UIColor.m_godReplyBgViewColor()
        self.upvoteView.delegate = self
        self.contentView.addSubview(self.upvoteView)
        self.upvoteView.snp.makeConstraints { (make) in
            make.top.equalTo(self.tagView.snp.centerY)
            make.right.equalTo(self.contentView).offset(-20)
            make.height.equalTo(26)
            make.width.equalTo(120)
        }
        
        self.godCommentView = UIView()
        self.godCommentView.backgroundColor = UIColor.m_godReplyBgViewColor()
        self.contentView.addSubview(self.godCommentView)
        self.godCommentView.snp.makeConstraints { (make) in
            make.top.equalTo(self.upvoteView.snp.bottom)
            make.right.equalTo(self.upvoteView)
            make.left.equalTo(self.contentLab)
            make.height.equalTo(50)
        }
        
        self.godTagView = UILabel()
        self.godTagView.layer.borderColor = UIColor.RGB(r: 237, g: 78, b: 72).cgColor
        self.godTagView.layer.borderWidth = 0.7
        self.godTagView.font = UIFont.LightDefaultFont(11)
        self.godTagView.text = "神"
        self.godTagView.textColor = UIColor.RGB(r: 237, g: 78, b: 72)
        self.godTagView.textAlignment = .center
        self.godCommentView.addSubview(self.godTagView)
        self.godTagView.snp.makeConstraints { (make) in
            make.top.equalTo(self.godCommentView.snp.top).offset(10)
            make.left.equalTo(self.godCommentView).offset(10)
            make.height.equalTo(18)
            make.width.equalTo(18)
        }
        
        self.godCommentLab = UILabel()
        self.godCommentLab.font = UIFont.LightDefaultFont(12)
        self.godCommentLab.text = "      我是一只大大鹅：无论什么选择，反正你最后不后悔就行"
        self.godCommentLab.textColor = UIColor.m_textDeepColor()
        self.godCommentLab.textAlignment = .left
        self.godCommentLab.numberOfLines = 0
        self.godCommentView.addSubview(self.godCommentLab)
        let godCommentLabHeight = StringSizeEngine.calculateStringHeight(self.godCommentLab.text!, width: self.contentLab.frame.width, font: self.godCommentLab.font)
        self.godCommentLab.snp.makeConstraints { (make) in
            make.top.equalTo(self.godCommentView.snp.top).offset(10)
            make.left.equalTo(self.godCommentView).offset(10)
            make.height.equalTo(godCommentLabHeight + 2)
            make.right.equalTo(self.godCommentView).offset(-10)
        }
        
        self.commentToolbarView = CommentToolbarView()
        self.commentToolbarView.backgroundColor = UIColor.white
        self.contentView.addSubview(self.commentToolbarView)
        self.commentToolbarView.snp.makeConstraints { (make) in
            make.top.equalTo(godCommentView.snp.bottom)
            make.left.right.equalTo(godCommentView)
            make.height.equalTo(50)
        }
        
        self.lineView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: 1))
        self.lineView.backgroundColor = UIColor.m_lineColor()
        self.contentView.addSubview(self.lineView)
        self.lineView.snp.remakeConstraints { (make) in
            make.height.equalTo(1)
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.bottom.equalTo(self)
        }
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    func refreshCellData(video:Video  , url:String  , indexPathRow:Int){

        self.contentLab.text = video.content!
        self.url = url
        self.video = video
        self.contentLab.tag = indexPathRow
        
        if video.isShow{
            self.contentLab.text = video.content!
            self.contentLab.numberOfLines = 0
        }else{
            self.contentLab.numberOfLines = 3
            Util.shared.setShowMoreBtnForLabel(label: self.contentLab, onLines: 3, title: "... [双击展开]", maxWidth: AppWidth - 80)
        }
        let contentHeight  = self.updateContentLabFrame(label: self.contentLab, width: AppWidth - 80)
        self.contentLab.frame = CGRect.init(x: self.nameLab.frame.minX, y: 55 , width: AppWidth - 80, height: contentHeight)
    }
    
    func tabButtonTapped(sender:UIButton , event:UIEvent){  //单击
        self.perform(#selector(sigleTapAction), with: sender, afterDelay: 0.2) //延时执行单击操作
    }
    
    func sigleTapAction (){
        let vc = VideoReplyDetailController()
        self.findNavigator().pushViewController(vc, animated: true)
    }
    
    func doubleTapAction (sender:UIButton , event:UIEvent){  //双击
        NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(sigleTapAction), object: sender)
        
        if self.contentLab.numberOfLines == 3{

            self.contentLab.numberOfLines = 0
            self.contentLab.text = self.video.content!
            
            let contentHeight = self.updateContentLabFrame(label: self.contentLab, width: AppWidth - 80)
            
            if delegate != nil{
                delegate.doubleLabel!(onDoubleCilck: self.contentLab, isShow: true , contentHeight: contentHeight)
            }
            
        }else{
            Util.shared.setShowMoreBtnForLabel(label: self.contentLab, onLines: 3, title: "... [双击展开]", maxWidth: AppWidth - 80)
            let contentHeight = self.updateContentLabFrame(label: self.contentLab, width: AppWidth - 80)
            
            if delegate != nil{
                delegate.doubleLabel!(onDoubleCilck: self.contentLab, isShow: false , contentHeight: contentHeight)
            }
        }
    }
    
    //MARK:点击神评论按钮回调
    func upvoteView(upvoteView: UpvoteView, didSelectButton button: UIButton, type: UpvoteViewType) {
        
    }
    
    
    private func updateContentLabFrame(label:UILabel , width:CGFloat)-> CGFloat{
        let contentHeight = StringSizeEngine.calculateStringHeight(label.text!, width: AppWidth - 80, font: label.font)
        
        return contentHeight
    }
    
    //MARK: 设置CollectionCell的样式
    fileprivate func initCollectionCellStyle(_ cellWidth:CGFloat , cellHeight:CGFloat , minimumLineSpacing:CGFloat) -> UICollectionViewFlowLayout{
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: cellWidth, height: cellHeight)  // 设置cell大小
        flowLayout.scrollDirection = UICollectionViewScrollDirection.vertical  //设置垂直滚动
        flowLayout.minimumLineSpacing = minimumLineSpacing  //设置行间距
        flowLayout.minimumInteritemSpacing = 0
        return flowLayout
    }
}
